package com.cdm.qf.autoconfig;

import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

import java.util.Map;

public class MapperScannerConfigurerRegistrar implements ImportBeanDefinitionRegistrar {

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {

//        RootBeanDefinition bd = new RootBeanDefinition();
//        bd.setBeanClass(MapperScannerConfigurer.class);

        BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(MapperScannerConfigurer.class);

        //向指定类型的对象中年添加指定名称的属性值
        Map<String, Object> annotationAttributes = importingClassMetadata.getAnnotationAttributes(EnableMapperScan.class.getName());
        String basePackage = (String) annotationAttributes.get("basePackage");

        builder.addPropertyValue("basePackage",basePackage);

        AbstractBeanDefinition bd = builder.getBeanDefinition();

        registry.registerBeanDefinition("xx",bd);
    }
}
